//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Quantity.h"
using namespace Cephei::QL::Experimental::Commodities;
#include <gen/QL/Experimental/Commodities/CommodityType.h>
#include <gen/QL/Experimental/Commodities/UnitOfMeasure.h>
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Experimental::Commodities::CQuantity::CQuantity (Cephei::QL::Experimental::Commodities::ICommodityType^ commodityType, Cephei::QL::Experimental::Commodities::IUnitOfMeasure^ unitOfMeasure, Double amount) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CCommodityType^ _CcommodityType;
    CUnitOfMeasure^ _CunitOfMeasure;
    try
    {
#ifdef HANDLE
        _phQuantity = NULL;
#endif
        _CcommodityType = safe_cast<CCommodityType^> (commodityType);
        _CcommodityType->Lock();
        QuantLib::CommodityType& _commodityType = static_cast<QuantLib::CommodityType&> (_CcommodityType->GetReference ()); 
        _CunitOfMeasure = safe_cast<CUnitOfMeasure^> (unitOfMeasure);
        _CunitOfMeasure->Lock();
        QuantLib::UnitOfMeasure& _unitOfMeasure = static_cast<QuantLib::UnitOfMeasure&> (_CunitOfMeasure->GetReference ()); 
        QuantLib::Real _amount = (QuantLib::Real)ValueHelper::Convert (amount); //d
        _ppQuantity = new boost::shared_ptr<QuantLib::Quantity> (new QuantLib::Quantity ( _commodityType,  _unitOfMeasure,  _amount ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcommodityType != nullptr) _CcommodityType->Unlock();
        if (_CunitOfMeasure != nullptr) _CunitOfMeasure->Unlock();
    }
}
Cephei::QL::Experimental::Commodities::CQuantity::CQuantity () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phQuantity = NULL;
#endif
        _ppQuantity = new boost::shared_ptr<QuantLib::Quantity> (new QuantLib::Quantity ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::CQuantity::CQuantity (boost::shared_ptr<QuantLib::Quantity>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phQuantity = NULL;
#endif
	_ppQuantity = &childNative;
    
}
Cephei::QL::Experimental::Commodities::CQuantity::CQuantity (QuantLib::Quantity& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phQuantity = NULL;
#endif
	_ppQuantity = new boost::shared_ptr<QuantLib::Quantity> (&childNative);
    
    _QuantityOwner = owner;
    
}

Cephei::QL::Experimental::Commodities::CQuantity::CQuantity (CQuantity^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phQuantity = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppQuantity = new boost::shared_ptr<QuantLib::Quantity> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Commodities::CQuantity::CQuantity (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phQuantity = NULL;
#endif
	if (!t->IsSubclassOf(CQuantity::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Commodities::CQuantity::CQuantity (QuantLib::Handle<QuantLib::Quantity>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phQuantity = &childNative;
	_ppQuantity = &static_cast<boost::shared_ptr<QuantLib::Quantity>>(childNative.currentLink());
    
    _QuantityOwner = owner;
}
Cephei::QL::Experimental::Commodities::CQuantity::CQuantity (QuantLib::Handle<QuantLib::Quantity> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phQuantity = &childNative;
	_ppQuantity = &static_cast<boost::shared_ptr<QuantLib::Quantity>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Commodities::CQuantity::CQuantity (QuantLib::Quantity childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phQuantity = NULL;
#endif
	_ppQuantity = new boost::shared_ptr<QuantLib::Quantity> (new QuantLib::Quantity (childNative));
    
}
#endif

Cephei::QL::Experimental::Commodities::CQuantity::~CQuantity ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppQuantity != NULL)
    {
	    delete _ppQuantity;
        _ppQuantity = NULL;
    }
}
Cephei::QL::Experimental::Commodities::CQuantity::!CQuantity ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppQuantity != NULL)
    {
	    delete _ppQuantity;
    }
}
QuantLib::Quantity& Cephei::QL::Experimental::Commodities::CQuantity::GetReference ()
{
    if (_ppQuantity == NULL) throw REFNEW NativeNullException ();
	return **_ppQuantity;
}
boost::shared_ptr<QuantLib::Quantity>& Cephei::QL::Experimental::Commodities::CQuantity::GetShared ()
{
    if (_ppQuantity == NULL) throw REFNEW NativeNullException ();
	return *_ppQuantity;
}
QuantLib::Quantity* Cephei::QL::Experimental::Commodities::CQuantity::GetPointer ()
{
    if (_ppQuantity == NULL) throw REFNEW NativeNullException ();
	return &**_ppQuantity;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Quantity>& Cephei::QL::Experimental::Commodities::CQuantity::GetHandle ()
{
	if (_phQuantity == NULL)
	{
		_phQuantity = new Handle<QuantLib::Quantity> (*_ppQuantity);
	}
	return *_phQuantity;
}
#endif
bool Cephei::QL::Experimental::Commodities::CQuantity::HasNative () 
{
	return (_ppQuantity != NULL);
}

Double Cephei::QL::Experimental::Commodities::CQuantity::Amount::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppQuantity)->amount ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::ICommodityType^ Cephei::QL::Experimental::Commodities::CQuantity::CommodityType::get ()
{
    try
    {
    	QuantLib::CommodityType& _rv = (QuantLib::CommodityType&)(*_ppQuantity)->commodityType ( );   
        Cephei::QL::Experimental::Commodities::CCommodityType^ _nrv = REFNEW Cephei::QL::Experimental::Commodities::CCommodityType (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::IQuantity^ Cephei::QL::Experimental::Commodities::CQuantity::Rounded::get ()
{
    try
    {
    	QuantLib::Quantity _rv = (QuantLib::Quantity)(*_ppQuantity)->rounded ( );   
        Cephei::QL::Experimental::Commodities::CQuantity^ _nrv = REFNEW Cephei::QL::Experimental::Commodities::CQuantity (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::IUnitOfMeasure^ Cephei::QL::Experimental::Commodities::CQuantity::UnitOfMeasure::get ()
{
    try
    {
    	QuantLib::UnitOfMeasure& _rv = (QuantLib::UnitOfMeasure&)(*_ppQuantity)->unitOfMeasure ( );   
        Cephei::QL::Experimental::Commodities::CUnitOfMeasure^ _nrv = REFNEW Cephei::QL::Experimental::Commodities::CUnitOfMeasure (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Commodities::IQuantity^ Cephei::QL::Experimental::Commodities::CQuantity_Factory::Create (Cephei::QL::Experimental::Commodities::ICommodityType^ commodityType, Cephei::QL::Experimental::Commodities::IUnitOfMeasure^ unitOfMeasure, Double amount)
{
    return REFNEW CQuantity ( commodityType,  unitOfMeasure,  amount);
}
Cephei::QL::Experimental::Commodities::IQuantity^ Cephei::QL::Experimental::Commodities::CQuantity_Factory::Create ()
{
    return REFNEW CQuantity ();
}
